package com.handinfo.uimenu.window;

import java.io.DataInputStream;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.game.ActorItem;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;


public class UILearnCollection extends UIAbstractWindow
{
    private byte m_state;//0采集 1遗忘
	private byte m_type;//采集界面类型   1草木  2金石 
	private String m_title_name;//标题名
	private byte m_learnlv;//学习等级
	private byte m_needlv;//需要等级
	private byte m_currencytype;//货币类型
	private long m_money;//货币数
    private int m_itemiconid;//道具ID
    private short m_itemnum;//道具数量
    private String m_itemname;//道具图标
    private String m_skillkey;//技能ID

	private int m_w_content = 200;//内容框的宽
	private int m_big_rect_h = 150;//上边大框的高
	private int m_small_rect_h = 70;//下边小框的高
	private int m_title_witdh = 120;//采集标题框宽
	private int m_title_height = 30;//采集标题框高
	private int m_title_offsetx = AbstractGameScreen.FONT_W >> 1;
	private String m_titleString;//标题内容
	private String m_needlvString;//需求等级内容
	private String m_moneyNumString;//需求货币内容
	private String m_itemInfoString;//需求道具信息内容
	private String[] m_smallInfoString;//下框内容信息
	private final int m_NumColour = 0xFF0000;//数字颜色
	private final int m_WordColour = 0xFFFFFF;//特殊字颜色
	private final int m_Colour = 0xFFFFFF;//标准字颜色
	
	public UILearnCollection(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
	}
    
	protected void init()
	{
	    StringBuffer buff = new StringBuffer(); 
	    String nameString = m_type == 1?"草木":"金石";
	    switch (m_state) {
			case 1:
				buff.append("采集"+nameString);
				m_title_name = buff.toString();
				buff.delete(0, m_title_name.length());
				break;
			case 2:
				m_title_name = "遗忘采集";
				break;
		}
        switch (m_state) {
			case 1://草木 金石
				buff.append("学习"+m_learnlv+"级采集"+Tools.CHANGE_ROW+nameString+Tools.CHANGE_ROW);
				m_titleString = buff.toString();
				m_title_witdh = AbstractGameScreen.FONT_W + AbstractGameScreen.s_font.stringWidth(m_titleString);
				buff.delete(0, m_titleString.length());
				//需求等级内容
				buff.append("需要等级："+m_needlv+"级");
				m_needlvString = buff.toString();
				buff.delete(0, m_needlvString.length());
				//需求货币内容
                 if( m_money == 0 ){
     				buff.append("需求金钱：无");	
				}else{
					buff.append("需求金钱：");	
					if( m_currencytype != 0 ){
						buff.append(m_money);
					}
				}
				m_moneyNumString = buff.toString();
				buff.delete(0, m_moneyNumString.length());
				//需求道具信息内容
				if( m_itemnum == 0 ){
					buff.append("需求道具：无");
				}else{
					buff.append("需求道具："+Tools.CHANGE_ROW + m_itemname+Tools.CHANGE_ROW +" x "+m_itemnum);
				}
				m_itemInfoString = buff.toString();
				buff.delete(0,m_itemInfoString.length());
				//下框内容信息
				buff.append("学习后您可以采集最高"+m_learnlv+"级的"+nameString+"。");
				m_smallInfoString = Tools.getStrArray(buff.toString(), (m_w_content - 2*AbstractGameScreen.FONT_W));
				buff = null;
				break;
			case 2: //遗忘采集
				buff.append(m_title_name+Tools.CHANGE_ROW+nameString+Tools.CHANGE_ROW);
				m_titleString = buff.toString();
				m_title_witdh = AbstractGameScreen.FONT_W + AbstractGameScreen.s_font.stringWidth(m_titleString);
				buff.delete(0, m_titleString.length());
				//花费金钱
				if( m_money == 0 ){
					buff.append("花费金钱：无");
				}else{
					buff.append("花费金钱：");
					if( m_currencytype != 0 ){
						buff.append(m_money);
					}
				}
				m_moneyNumString = buff.toString();
				buff.delete(0, m_moneyNumString.length());
				//花费道具
				if( m_itemnum == 0 ){
					buff.append("花费道具：无");
				}else{
					buff.append("花费道具："+Tools.CHANGE_ROW + m_itemname+Tools.CHANGE_ROW +" x "+m_itemnum);
				}	
				m_itemInfoString = buff.toString();
				buff.delete(0,m_itemInfoString.length());
				//下框内容信息
				buff.append("选择后您将遗忘当前掌握的采集技能。");
				m_smallInfoString = Tools.getStrArray(buff.toString(), (m_w_content - 2*AbstractGameScreen.FONT_W));
				buff = null;
				break;
		}
	}
	
	protected void paintContent(Graphics g)
	{
		//绘制背景
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, m_title_name);
		final int tempH = 5;
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_y + UITools.INITY_CONTENT + tempH;
		//绘制上框
		UITools.paintContentRect(g, tempX, tempY, m_w_content, m_big_rect_h);
		//绘制下框
		tempY += (m_big_rect_h+tempH);
		//绘制上框
		UITools.paintContentRect(g, tempX, tempY, m_w_content, m_small_rect_h);
		//绘制标题
		tempX += (( m_w_content - m_title_witdh) >> 1);
		tempY =  m_y + UITools.INITY_CONTENT + tempH *3;
		UITools.paintLabel(g, tempX, tempY, m_title_witdh, m_title_height, true);
		//绘制标题内容
		paintTitleInfo(g,tempX,tempY);
		//绘制上下框内容
		switch(m_state){
			case 1:
				paintLearnCollection(g);
				break;
			case 2:
				paintForgetCollection(g);
				break;		
		}
	}
    
	/** 绘制标题 */
	protected void paintTitleInfo(Graphics g,int x,int y)
	{
		if( m_titleString != null ){
			x += m_title_offsetx;
			y += ((m_title_height - AbstractGameScreen.FONT_H) >> 1);
		    specialString(g, x, y, m_titleString, m_NumColour, m_WordColour, m_Colour);
		}
	}
	
	/** 绘制采集内容 */
	protected void paintLearnCollection(Graphics g)
	{
		final int tempH = 5;
		int tempX = m_x + UITools.INITX_CONTENT+AbstractGameScreen.FONT_W;
		int tempY = m_y + UITools.INITY_CONTENT + m_title_height + tempH *6;
		//绘制需求等级内容
		if( m_needlvString != null ){
			specialString(g, tempX, tempY, m_needlvString, m_NumColour, m_WordColour,m_Colour);
		}
		//绘制需求金钱
		if( m_moneyNumString != null ){
			tempY += AbstractGameScreen.FONT_H + tempH;
			specialString(g, tempX, tempY, m_moneyNumString, m_NumColour, m_WordColour,m_Colour);
			tempX += AbstractGameScreen.s_font.stringWidth( m_moneyNumString+" ");
			if(m_money != 0){
				switch( m_currencytype ){//货币类型
					case 0://游戏币
						UITools.paintMoneyGameLT(g, tempX, tempY, m_money, true);
						break;
					case 1://元宝					
						g.drawImage(UIManager.IMAGE_MONEY_RMB, tempX, tempY, 0);
						break;
					case 2://银票				
						g.drawImage(UIManager.IMAGE_YINPIAO, tempX, tempY, 0);
					    break;
				}
			}	
		}
		//绘制需求道具
	    if( m_itemInfoString != null ){
	    	tempX = m_x + UITools.INITX_CONTENT + AbstractGameScreen.FONT_W;
	    	tempY += AbstractGameScreen.FONT_H + tempH;
			specialString(g, tempX, tempY, m_itemInfoString, m_NumColour, m_WordColour,m_Colour);
			tempX += AbstractGameScreen.s_font.stringWidth(m_itemInfoString+" "); 
			ActorItem.drawIcon(g, tempX, tempY, 0, 0, m_itemiconid, 0, 0, false);
	    }
	    //绘制下框内容
	    if( m_smallInfoString != null ){
	    	tempX = m_x + UITools.INITX_CONTENT+AbstractGameScreen.FONT_W;
	    	tempY = m_y + UITools.INITY_CONTENT + tempH*3 + m_big_rect_h;
	    	int size = m_smallInfoString.length;
	    	final int wordH = 2;
	    	for(int i=0;i<size;i++){
	    		specialString(g, tempX, tempY+i*(AbstractGameScreen.FONT_H + wordH), m_smallInfoString[i],  m_NumColour, m_WordColour,m_Colour);
	    	}
	    }
	}
	
	//绘制遗忘采集内容
	protected void paintForgetCollection(Graphics g)
	{
		final int tempH = 5;
		int tempX = m_x + UITools.INITX_CONTENT + AbstractGameScreen.FONT_W;
		int tempY = m_y + UITools.INITY_CONTENT + m_title_height + tempH * 6;
		//绘制需求金钱
		if (m_moneyNumString != null) {
			tempY += AbstractGameScreen.FONT_H + tempH;
			specialString(g, tempX, tempY, m_moneyNumString, m_NumColour, m_WordColour, m_Colour);
			tempX += AbstractGameScreen.s_font.stringWidth( m_moneyNumString+" ");
			if( m_money != 0){
				switch( m_currencytype ){//货币类型
					case 0://游戏币
						UITools.paintMoneyGameLT(g, tempX, tempY, m_money, true);
						break;
					case 1://元宝				
						g.drawImage(UIManager.IMAGE_MONEY_RMB, tempX, tempY, 0);
						break;
					case 2://银票					
						g.drawImage(UIManager.IMAGE_YINPIAO, tempX, tempY, 0);
					    break;
				}
			}
		}
		//绘制需求道具
		if (m_itemInfoString != null) {
			tempX = m_x + UITools.INITX_CONTENT + AbstractGameScreen.FONT_W;
			tempY += AbstractGameScreen.FONT_H + tempH;
			specialString(g, tempX, tempY, m_itemInfoString, m_NumColour, m_WordColour, m_Colour);
			tempX += AbstractGameScreen.s_font.stringWidth(m_itemInfoString + " ");
			ActorItem.drawIcon(g, tempX, tempY, 0, 0, m_itemiconid, 0, 0, false);
		}
		//绘制下框内容
		if (m_smallInfoString != null) {
			tempX = m_x + UITools.INITX_CONTENT + AbstractGameScreen.FONT_W;
			tempY = m_y + UITools.INITY_CONTENT + tempH * 3 + m_big_rect_h;
			int size = m_smallInfoString.length;
			final int wordH = 2;
			for (int i = 0; i < size; i++) {
				specialString(g, tempX, tempY + i * (AbstractGameScreen.FONT_H + wordH), m_smallInfoString[i],
						m_NumColour, m_WordColour, m_Colour);
			}
		}
	}
	
	//绘制带颜色数字字符串
	protected void specialString(Graphics g,int x,int y,String Str,int NumColour,int WordColour,int Colour){
		if( Str != null ){	
			int size = Str.length();
			int width = 0;
			boolean flag = false;
			for(int i=0;i<size;i++){
				char temp = Str.charAt(i);
				if( temp == Tools.CHANGE_ROW ){
					flag = flag?false:true;
					g.setColor(WordColour);					
				}else{
					if( temp >=48 && temp <= 57 ){
						g.setColor(NumColour);
					}else {
						if(!flag){
							g.setColor(Colour);
						}
					}
					g.drawChar(temp, x+width, y, 0);
					width += AbstractGameScreen.s_font.charWidth(temp);
				}	
			}
		}
	} 
	
	protected void keyLogic()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
				|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			switch (m_state) {
				case 1://学习
					Tools.debugPrintln("学习");
					GameManager.getInstance().m_game_message.sendLearnCollectionInofs(m_skillkey);
					break;
				case 2://遗忘
					Tools.debugPrintln("遗忘");
					GameManager.getInstance().m_game_message.sendForgetCollectionInofs(m_skillkey);
					break;
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		}
	}

	/**
	 * 接收采集
	 * */
	public static final void recvLearnCollection(DataInputStream dis)
	{
		try {
			Tools.debugPrintln("recvLearnCollection");
			byte state = dis.readByte();
			String temp = "";			
			switch(state){
				case 0://失败
					temp = "操作失败！";
					break;
				case 1://成功
					temp = null;
					break;
				case 2:
					temp = "如果您要学习此技能，需要先遗忘之前的技能。";
					break;
				case 3:
					temp = "您已经学习到最高等级。";
					break;
			}
			if( temp == null ){
				UILearnCollection win = (UILearnCollection) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_ID_COLLECTION);
				win.m_state = 1;
				win.m_type = dis.readByte();
			    win.m_skillkey = dis.readUTF();//技能ID
				win.m_learnlv = dis.readByte();//学习等级
				win.m_needlv = dis.readByte();//需要等级
				win.m_currencytype = dis.readByte();//货币类型
				win.m_money = dis.readLong();//货币数
				win.m_itemiconid = dis.readInt();//道具ICON ID
				win.m_itemnum = dis.readShort();//道具数量
				win.m_itemname = dis.readUTF();//道具名称
				win.init();
				UIManager.getInstance().addWindow(win);				
			}else{
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, temp);
			}
			dis.close();
			dis = null;
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}
	
	/**
	 * 接收遗忘 
	 * */
	public static final void recvForgetCollection(DataInputStream dis)
	{
			try {
				Tools.debugPrintln("recvForgetCollection");
				byte state = dis.readByte();
				String temp = "";			
				switch(state){
					case 0://失败
						temp = "操作失败！";
						break;
					case 1://成功
						temp = null;
						break;
					case 2://
						temp = "您还没有学习技能不需要遗忘。"; 
						break;
				}
				if( temp == null ){
					UILearnCollection win = (UILearnCollection) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_ID_COLLECTION);
					win.m_state = 2;
					win.m_type = dis.readByte();
					win.m_skillkey = dis.readUTF();//技能ID
					win.m_currencytype = dis.readByte();//货币类型
					win.m_money = dis.readLong();//货币数
					win.m_itemiconid = dis.readInt();//道具ICON ID
					win.m_itemnum = dis.readShort();//道具数量
					win.m_itemname = dis.readUTF();//道具名称
					win.init();
					UIManager.getInstance().addWindow(win);				
				}else{
					GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, temp);
				}
				dis.close();
				dis = null;
			}
			catch (Exception e) {
				Tools.debug(e);
			}
	}
	
	/** 接收确定学习采集返回 */
	public static final void recvLearnCollectionOK(DataInputStream dis)
	{
		try {
			Tools.debugPrintln("recvLearnCollectionOK");
			byte state = dis.readByte();
			String temp = "";
			switch(state){
				case 0://成功
					UILearnCollection win = (UILearnCollection)UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_ID_COLLECTION);
					if( win != null ){
						temp = "恭喜您成功学习"+win.m_title_name+"。";
						win.close();
					}
					break;
				case 1:
					temp = "您的等级不足！";
					break;
				case 2:
					temp = "您的金钱不足！";
					break;
				case 3:
					temp = "您的道具不足！";
					break;
				default:
					GameManager.getInstance().m_game_message.unlockNetwork();
					return;
			}
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, temp);
			GameManager.getInstance().m_game_message.unlockNetwork();
		}
		catch (Exception e) {
			GameManager.getInstance().m_game_message.unlockNetwork();
			Tools.debug(e);
		}
	}
	
	/** 接收确定遗忘采集返回 */
	public static final void recvForgetCollectionOK(DataInputStream dis)
	{
		try {
			Tools.debugPrintln("recvForgetCollectionOK");
			byte state = dis.readByte();
			String temp = "";
			switch(state){
				case 0://成功
					UILearnCollection win = (UILearnCollection)UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_ID_COLLECTION);
					if( win != null ){
						temp = "恭喜您"+win.m_title_name+"成功。";
						win.close();
					}					
					break;
				case 1:
					temp = "您的金钱不足！";
					break;
				case 2:
					temp = "您的道具不足！";
					break;
				default:
					GameManager.getInstance().m_game_message.unlockNetwork();
					return;
			}
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, temp);
			GameManager.getInstance().m_game_message.unlockNetwork();
		}
		catch (Exception e) {
			GameManager.getInstance().m_game_message.unlockNetwork();
			Tools.debug(e);
		}
	}
	
}
